/**
 * 
 */
package com.alstudio.ui.module.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.utils.android.keyboard.ALKeyBoardManager;
import com.alstudio.utils.formater.string.StringUtils;
import com.alstudio.utils.log.ALLog;
import com.alstudio.view.edittext.ALEditText;
import com.alstudio.view.edittext.ALEditText.OnALEditorActionListener;
import com.alstudio.view.toast.MyToastUtil;
import com.loovee.imaohu.R;

/**
 * 修改密码
 * 
 * @author alonso lee
 * 
 */
public class ChangePasswordActivity extends TitleBarActivity implements
		OnALEditorActionListener {

	// 老密码输入框
	private ALEditText origin_pwd;
	// 新密码
	private ALEditText new_pwd;
	// 確認密碼
	private ALEditText confirm_pwd;

	private String newPassWord;

	// 密码不符合要求
	private final int CHANGE_PWD_ERROR_404 = 404;
	// 老密码不对
	private final int CHANGE_PWD_ERROR_402 = 402;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(this, getClass().getSimpleName());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#setupView(android.content.Context,
	 * java.lang.String)
	 */
	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarActivityContentView(R.layout.change_pwd_layout);
		setCenterTextViewMessage(R.string.TxtChangePwd);
		rightUseImageButton(true);
		setRightButtonImageSrc(R.drawable.friend_title_icon_confirm);

//		setRightButtonBackground(R.drawable.friend_title_icon_confirm);
		setRightButtonClickListener(this);
		getRightButton().setEnabled(false);
		origin_pwd = (ALEditText) findViewById(R.id.origin_pwd);
		new_pwd = (ALEditText) findViewById(R.id.new_pwd);
		confirm_pwd = (ALEditText) findViewById(R.id.confirm_pwd);
		origin_pwd.setALEditorActionListener(this);
		new_pwd.setALEditorActionListener(this);
		confirm_pwd.setALEditorActionListener(this);
		setViewOnclickListener(R.id.forgot_password_view, this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#initData()
	 */
	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.base.BaseActivity#handleChangePasswordEvent(com.alstudio
	 * .pojo.ALXmppEvent)
	 */
	@Override
	public void handleChangePasswordEvent(ALXmppEvent data) {
		// TODO Auto-generated method stub
		super.handleChangePasswordEvent(data);
		dismissLoadingProgress();
		if (data.getResponseCode() == ALXmppEvent.RESPONSE_OK) {
			if (!TextUtils.isEmpty(newPassWord)) {
				ALLocalEnv.getLocalEnv().getMyAccount()
						.setPassWord(newPassWord);
				ALLocalEnv.getLocalEnv().getDatabase()
						.saveMyAccount(ALLocalEnv.getLocalEnv().getMyAccount());
				ALLog.d("保存新密碼 " + newPassWord);
				finish();
			}
		} else {
			switch (data.getResponseCode()) {
			case CHANGE_PWD_ERROR_404:
				// 密码不符合要求
				MyToastUtil.getInstance().showToastOnCenter(
						"" + CHANGE_PWD_ERROR_404);
				break;
			case CHANGE_PWD_ERROR_402:
				// 当前密码不正确
				MyToastUtil.getInstance().showToastOnCenter(
						getString(R.string.TxtOriginPwdIncorrect));
				break;
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.view.edittext.ALEditText.OnALEditorActionListener#onEditorAction
	 * ()
	 */
	@Override
	public void onEditorAction() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.view.edittext.ALEditText.OnALEditorActionListener#onContentClear
	 * ()
	 */
	@Override
	public void onContentClear() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.view.edittext.ALEditText.OnALEditorActionListener#
	 * onContentChange(java.lang.String)
	 */
	@Override
	public void onContentChange(String content) {
		// TODO Auto-generated method stub
		boolean isFilled = content.length() != 0;
		getRightButton().setEnabled(isFilled);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.forgot_password_view:
			// 点击忘记密码
			Intent i = new Intent(this, ForgotPassActivity.class);
			startActivity(i);
			break;
		}
	}
	
	
	/* (non-Javadoc)
	 * @see com.alstudio.ui.base.TitleBarActivity#invokeRightBtnClick()
	 */
	@Override
	protected void invokeRightBtnClick() {
		// TODO Auto-generated method stub
		super.invokeRightBtnClick();
		invokeCommitClick();
	}

	private void invokeCommitClick() {
		if (!ALLocalEnv.isSendXmppDataEnable())
			return;
		String ori = origin_pwd.getText().toString().trim();
		String newPwd = new_pwd.getText().toString().trim();
		String confirmPwd = confirm_pwd.getText().toString().trim();

		// 密码长度不够
		if (ori.length() < 6 || newPwd.length() < 6 || confirmPwd.length() < 6) {
			MyToastUtil.getInstance().showToastOnCenter(
					getString(R.string.TxtPasswordTooShort));
			return;
		}

		// 兩次輸入不一致
		if (!newPwd.equals(confirmPwd)) {
			MyToastUtil.getInstance().showToastOnCenter(
					getString(R.string.TxtNewPwdIncorrect));
			return;
		}

		// 检测是否为简单密码
		if (StringUtils.isSimplePassword(newPwd)) {
			MyToastUtil.getInstance().showToastOnCenter(
					getString(R.string.TxtPasswordSimple));
			return;
		}
		newPassWord = newPwd;
		ALUiXmppRequestCreator.postALUiChangePassword(ori, newPwd);
		ALKeyBoardManager.dismissKeyBoard(this);
		showLoadingProgress();
	}

}
